//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.farm.doc.server.impl;

import com.farm.authority.domain.User;
import com.farm.core.auth.domain.LoginUser;
import com.farm.core.sql.query.DBRule;
import com.farm.core.sql.query.DBSort;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.time.TimeTool;
import com.farm.doc.dao.FarmDocDaoInter;
import com.farm.doc.dao.FarmDocRelationDaoInter;
import com.farm.doc.dao.FarmDocenjoyDaoInter;
import com.farm.doc.dao.FarmDocfileDaoInter;
import com.farm.doc.dao.FarmDocruninfoDaoInter;
import com.farm.doc.dao.FarmDocruninfoDetailDaoInter;
import com.farm.doc.dao.FarmDoctextDaoInter;
import com.farm.doc.dao.FarmDoctypeDaoInter;
import com.farm.doc.dao.FarmRfDoctextfileDaoInter;
import com.farm.doc.dao.FarmRfDoctypeDaoInter;
import com.farm.doc.dao.FarmtopDaoInter;
import com.farm.doc.domain.Doc;
import com.farm.doc.domain.FarmDocfile;
import com.farm.doc.domain.FarmDocruninfo;
import com.farm.doc.domain.FarmDoctext;
import com.farm.doc.domain.FarmDoctype;
import com.farm.doc.domain.FarmRfDoctextfile;
import com.farm.doc.domain.FarmRfDoctype;
import com.farm.doc.domain.ex.DocEntire;
import com.farm.doc.exception.CanNoDeleteException;
import com.farm.doc.exception.CanNoReadException;
import com.farm.doc.exception.CanNoWriteException;
import com.farm.doc.exception.DocNoExistException;
import com.farm.doc.server.FarmDocIndexInter;
import com.farm.doc.server.FarmDocManagerInter;
import com.farm.doc.server.FarmDocOperateRightInter;
import com.farm.doc.server.FarmDocRunInfoInter;
import com.farm.doc.server.FarmDocTypeInter;
import com.farm.doc.server.FarmDocgroupManagerInter;
import com.farm.doc.server.FarmFileIndexManagerInter;
import com.farm.doc.server.FarmFileManagerInter;
import com.farm.doc.server.UsermessageServiceInter;
import com.farm.doc.server.FarmDocOperateRightInter.POP_TYPE;
import com.farm.doc.server.FarmFileManagerInter.FILE_APPLICATION_TYPE;
import com.farm.doc.server.commons.DocumentConfig;
import com.farm.doc.server.commons.FarmDocFiles;
import com.farm.doc.server.plus.FarmTypePopServerInter;
import com.farm.doc.server.plus.domain.DocAudit;
import com.farm.doc.server.plus.domain.Doctypepop;
import com.farm.doc.util.DocTagUtils;
import com.farm.doc.util.HtmlUtils;
import com.farm.parameter.FarmParameterService;
import com.farm.util.web.FarmFormatUnits;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FarmDocManagerImpl implements FarmDocManagerInter {
	private static final Logger log = Logger.getLogger(FarmDocManagerImpl.class);
	@Resource
	private FarmDocTypeInter farmDocTypeManagerImpl;
	@Resource
	private FarmDocDaoInter farmDocDao;
	@Resource
	private FarmDocfileDaoInter farmDocfileDao;
	@Resource
	private FarmDoctextDaoInter farmDoctextDao;
	@Resource
	private FarmTypePopServerInter farmTypePopServerImpl;
	@Resource
	private FarmRfDoctextfileDaoInter farmRfDoctextfileDao;
	@Resource
	private FarmRfDoctypeDaoInter farmRfDoctypeDao;
	@Resource
	private FarmDoctypeDaoInter farmDoctypeDao;
	@Resource
	private FarmDocruninfoDaoInter farmDocruninfoDao;
	@Resource
	private FarmDocenjoyDaoInter farmDocenjoyDao;
	@Resource
	private FarmDocOperateRightInter farmDocOperate;
	@Resource
	private FarmDocgroupManagerInter farmDocgroupManagerImpl;
	@Resource
	private FarmDocruninfoDetailDaoInter farmDocruninfoDetailDao;
	@Resource
	private FarmDocRunInfoInter farmDocRunInfoImpl;
	@Resource
	private FarmFileManagerInter farmFileServer;
	@Resource
	private FarmtopDaoInter farmtopDaoImpl;
	@Resource
	private FarmDocOperateRightInter farmDocOperateRightImpl;
	@Resource
	private UsermessageServiceInter usermessageServiceImpl;
	@Resource
	private FarmDocIndexInter farmDocIndexManagerImpl;
	@Resource
	private FarmFileIndexManagerInter farmFileIndexManagerImpl;
	@Resource
	private FarmDocRelationDaoInter farmDocRelationDao;

	public FarmDocManagerImpl() {
	}

	@Transactional
	public DocEntire createDoc(DocEntire entity, LoginUser user) {
		try {
			FarmDocruninfo runinfo = new FarmDocruninfo();
			runinfo.setHotnum(Integer.valueOf(0));
			runinfo.setLastvtime(TimeTool.getTimeDate14());
			runinfo.setPraiseno(Integer.valueOf(0));
			runinfo.setPraiseyes(Integer.valueOf(0));
			runinfo.setVisitnum(Integer.valueOf(0));
			runinfo.setAnsweringnum(Integer.valueOf(0));
			runinfo.setEvaluate(Integer.valueOf(0));
			runinfo = this.farmDocruninfoDao.insertEntity(runinfo);
			entity.getDoc().setRuninfoid(runinfo.getId());
			entity.setRuninfo(runinfo);
			entity.getTexts().setPstate("1");
			entity.getTexts().setPcontent("CREAT");
			entity.setTexts(this.farmDoctextDao.insertEntity(entity.getTexts()));
			entity.getDoc().setTextid(entity.getTexts().getId());
			if ((entity.getDoc().getDocdescribe() == null || entity.getDoc().getDocdescribe().trim().length() <= 0)
					&& entity.getTexts() != null) {
				String html = HtmlUtils.HtmlRemoveTag(entity.getTexts().getText1());
				if (html != null) {
					entity.getDoc().setDocdescribe(html.length() > 128 ? html.substring(0, 128) : html);
				}
			}

			if ((entity.getDoc().getTagkey() == null || entity.getDoc().getTagkey().trim().length() <= 0)
					&& "TRUE".equals(FarmParameterService.getInstance().getParameter("config.sys.tags.autocreate")
							.toUpperCase())) {
				entity.getDoc().setTagkey(DocTagUtils.getTags(entity.getTexts().getText1()));
			}

			entity.getDoc().setCtime(TimeTool.getTimeDate14());
			entity.getDoc().setEtime(TimeTool.getTimeDate14());
			entity.getDoc().setCuser(user.getId());
			entity.getDoc().setEuser(user.getId());
			entity.getDoc().setCusername(user.getName());
			entity.getDoc().setEusername(user.getName());
			entity.getDoc().setPubtime(
					entity.getDoc().getPubtime().replaceAll("-", "").replaceAll(":", "").replaceAll(" ", ""));
			if (entity.getDoc().getReadpop() == null) {
				entity.getDoc().setReadpop(POP_TYPE.PUB.getValue());
			}

			if (entity.getDoc().getWritepop() == null) {
				entity.getDoc().setWritepop(POP_TYPE.PUB.getValue());
			}

			if (entity.getDoc().getAuthor() == null || entity.getDoc().getAuthor().isEmpty()) {
				entity.getDoc().setAuthor(user.getName());
			}

			if (entity.getType() != null && !entity.getType().getAuditpop().equals("0")) {
				entity.getDoc().setState("2");
			}

			if (entity.getDoc().getDocgroupid() != null && entity.getDoc().getDocgroupid().isEmpty()) {
				entity.getDoc().setDocgroupid((String) null);
			}

			entity.setDoc(this.farmDocDao.insertEntity(entity.getDoc()));
			this.farmFileServer.submitFile(entity.getDoc().getImgid(), FILE_APPLICATION_TYPE.KNOW_IMG.getValue());
			if (entity.getType() != null && !entity.getType().getAuditpop().equals("0")) {
				DocAudit audit = this.farmTypePopServerImpl.auditHandleForDocCreate(entity.getDoc().getId(),
						entity.getTexts().getId(), user);
				entity.setAudit(audit);
				entity.setAuditTemp(entity.getTexts());
				this.usermessageServiceImpl.sendMessage(
						this.farmDocTypeManagerImpl.getPopTypeAudtUserIds(entity.getType().getId()),
						this.farmTypePopServerImpl.getMessageForNeedAudit(entity.getDoc().getTitle()),
						this.farmTypePopServerImpl.getMessageTitleForNeedAudit(entity.getDoc().getTitle()));
			}

			if (entity.getType() != null) {
				this.farmRfDoctypeDao
						.insertEntity(new FarmRfDoctype(entity.getType().getId(), entity.getDoc().getId()));
			}

			entity.setDoc(this.farmDocDao.insertEntity(entity.getDoc()));
			this.farmFileServer.submitFile(entity.getDoc().getImgid(), FILE_APPLICATION_TYPE.KNOW_IMG.getValue());
			List<String> files = FarmDocFiles.getFilesIdFromHtml(entity.getTexts().getText1());
			Iterator var5 = files.iterator();

			while (var5.hasNext()) {
				String id = (String) var5.next();
				this.farmRfDoctextfileDao.insertEntity(new FarmRfDoctextfile(entity.getDoc().getId(), id));
				this.farmFileServer.submitFile(id, FILE_APPLICATION_TYPE.KNOW_OR_RESOURCE_FILE.getValue());
			}

			List<FarmDocfile> files2 = entity.getFiles();
			Iterator var6 = files2.iterator();

			while (var6.hasNext()) {
				FarmDocfile file = (FarmDocfile) var6.next();
				this.farmRfDoctextfileDao.insertEntity(new FarmRfDoctextfile(entity.getDoc().getId(), file.getId()));
				this.farmFileServer.submitFile(file.getId(), FILE_APPLICATION_TYPE.KNOW_OR_RESOURCE_FILE.getValue());
			}

			if (entity.getDoc().getDocgroupid() != null && !entity.getDoc().getDocgroupid().isEmpty()) {
				entity.setGroup(this.farmDocgroupManagerImpl.getFarmDocgroup(entity.getDoc().getDocgroupid()));
			} else {
				entity.getDoc().setDocgroupid((String) null);
			}

			if (entity.getType() == null || entity.getType().getAuditpop().equals("0")) {
				this.farmDocIndexManagerImpl.addLuceneIndex(entity);
			}

			return entity;
		} catch (Exception var7) {
			var7.printStackTrace();
			throw new RuntimeException("farmDoc文档创建失败：" + var7 + var7.getMessage());
		}
	}

	@Transactional
	public DocEntire editDocByUser(DocEntire entity, String editNote, LoginUser user) throws CanNoWriteException {
		Doc entity2 = this.farmDocDao.getEntity(entity.getDoc().getId());
		if (!this.farmDocOperate.isWrite(user, entity2)) {
			throw new CanNoWriteException();
		} else {
			return this.editDoc(entity, editNote, user);
		}
	}

	@Transactional
	public DocEntire editDoc(DocEntire paradoce, String editNote, LoginUser user) {
		DocEntire newDoce = this.getDoc(paradoce.getDoc().getId());
		DocEntire oldDoce = null;
		String oldImgid = newDoce.getDoc().getImgid();

		try {
			oldDoce = (DocEntire) BeanUtils.cloneBean(newDoce);
		} catch (Exception var14) {
			throw new RuntimeException(var14);
		}

		newDoce.getDoc().setEtime(TimeTool.getTimeDate14());
		newDoce.getDoc().setEuser(user.getId());
		newDoce.getDoc().setEusername(user.getName());
		if (paradoce.getDoc().getPcontent() != null) {
			newDoce.getDoc().setPcontent(paradoce.getDoc().getPcontent());
		}

		if (paradoce.getDoc().getReadpop() != null) {
			newDoce.getDoc().setReadpop(paradoce.getDoc().getReadpop());
		}

		if (paradoce.getDoc().getWritepop() != null) {
			newDoce.getDoc().setWritepop(paradoce.getDoc().getWritepop());
		}

		if (paradoce.getDoc().getState() != null) {
			newDoce.getDoc().setState(paradoce.getDoc().getState());
		}

		if (paradoce.getDoc().getDocgroupid() != null && !paradoce.getDoc().getDocgroupid().isEmpty()) {
			newDoce.getDoc().setDocgroupid(paradoce.getDoc().getDocgroupid());
		}

		if (paradoce.getDoc().getImgid() != null && !paradoce.getDoc().getImgid().isEmpty()) {
			newDoce.getDoc().setImgid(paradoce.getDoc().getImgid());
		}

		if (paradoce.getDoc().getTopleve() != null) {
			newDoce.getDoc().setTopleve(paradoce.getDoc().getTopleve());
		}

		if (paradoce.getDoc().getPubtime() != null) {
			newDoce.getDoc().setPubtime(
					paradoce.getDoc().getPubtime().replaceAll(" ", "").replaceAll("-", "").replaceAll(":", ""));
		}

		if (paradoce.getDoc().getSource() != null) {
			newDoce.getDoc().setSource(paradoce.getDoc().getSource());
		}

		if (paradoce.getDoc().getTagkey() != null) {
			newDoce.getDoc().setTagkey(paradoce.getDoc().getTagkey());
		}

		if (paradoce.getDoc().getShorttitle() != null) {
			newDoce.getDoc().setShorttitle(paradoce.getDoc().getShorttitle());
		}

		if (paradoce.getDoc().getDomtype() != null) {
			newDoce.getDoc().setDomtype(paradoce.getDoc().getDomtype());
		}

		if (newDoce.getDoc().getAuthor() == null) {
			newDoce.getDoc().setAuthor(newDoce.getDoc().getCusername());
		}

		if (paradoce.getDoc().getAuthor() != null) {
			newDoce.getDoc().setAuthor(paradoce.getDoc().getAuthor());
		}

		if (paradoce.getTexts() != null) {
			String html = HtmlUtils.HtmlRemoveTag(paradoce.getTexts().getText1());
			newDoce.getDoc().setDocdescribe(html.length() > 128 ? html.substring(0, 128) : html);
		}

		if (!paradoce.getDoc().getDocdescribe().isEmpty()) {
			newDoce.getDoc().setDocdescribe(paradoce.getDoc().getDocdescribe());
		}

		if (paradoce.getDoc().getTitle() != null) {
			newDoce.getDoc().setTitle(paradoce.getDoc().getTitle());
		}

		FarmDoctype oldType = this.farmRfDoctypeDao.getDocType(paradoce.getDoc().getId());
		FarmDoctype newType = paradoce.getType();
		if (newType != null) {
			newDoce.setType(newType);
			List<FarmDoctype> typelist = new ArrayList();
			typelist.add(newType);
			newDoce.setCurrenttypes(typelist);
		}

		if (newType == null) {
			newType = oldType;
		}

		this.farmDocDao.editEntity(newDoce.getDoc());
		
		try {
			this.farmFileServer.updateFileState(oldImgid, newDoce.getDoc().getImgid(), user);
			
		} catch (Exception e) {
			log.error(">>>>>>>>>>>>>>>>>>>>>>>> 更新文件状态异常",e);
		}
		if (newType != null && !newType.getAuditpop().equals("0")) {
			this.editDocAuditHandle(newType, oldType, newDoce, paradoce, user, editNote);
		} else {
			this.usermessageServiceImpl.sendMessage(this.farmDocRunInfoImpl.getDocJoinUserIds(newDoce.getDoc().getId()),
					"知识‘" + newDoce.getDoc().getTitle() + "’有新版本提交", "知识更新消息");
			this.insertDocTextVersionForCurrent(newDoce, paradoce, user, editNote);
		}

		List<String> list = FarmDocFiles.getFilesIdFromHtml(paradoce.getTexts().getText1());
		Iterator var11 = list.iterator();

		while (var11.hasNext()) {
			String id = (String) var11.next();
			List<DBRule> listRules = new ArrayList();
			listRules.add(new DBRule("FILEID", id, "="));
			listRules.add(new DBRule("DOCID", newDoce.getDoc().getId(), "="));
			FarmDocfile file = this.farmFileServer.getFile(id);
			if (file != null) {
				this.farmRfDoctextfileDao.deleteEntitys(listRules);
				this.farmRfDoctextfileDao.insertEntity(new FarmRfDoctextfile(newDoce.getDoc().getId(), id));
				this.farmFileServer.submitFile(id, FILE_APPLICATION_TYPE.KNOW_OR_RESOURCE_FILE.getValue());
			} else {
				log.error("file(" + id + ") not exist at database!");
			}
		}

		List<FarmDocfile> files2 = paradoce.getFiles();
		Iterator var20 = files2.iterator();

		while (var20.hasNext()) {
			FarmDocfile file = (FarmDocfile) var20.next();
			this.farmRfDoctextfileDao.insertEntity(new FarmRfDoctextfile(newDoce.getDoc().getId(), file.getId()));
			this.farmFileServer.submitFile(file.getId(), FILE_APPLICATION_TYPE.KNOW_OR_RESOURCE_FILE.getValue());
		}

		List<FarmRfDoctype> selectEntitys = this.farmRfDoctypeDao
				.selectEntitys((new DBRule("DOCID", paradoce.getDoc().getId(), "=")).getDBRules());
		if (paradoce.getType() != null) {
			if (list != null && list.size() > 0) {
				FarmRfDoctype farmRfDoctype = (FarmRfDoctype) selectEntitys.get(0);
				farmRfDoctype.setTypeid(paradoce.getType().getId());
				this.farmRfDoctypeDao.editEntity(farmRfDoctype);
			} else {
				this.farmRfDoctypeDao
						.insertEntity(new FarmRfDoctype(paradoce.getType().getId(), paradoce.getDoc().getId()));
			}
		}

		if (newType != null && !newType.getAuditpop().equals("0")) {
			if (!newType.getId().equals(oldType.getId())) {
				this.farmDocIndexManagerImpl.delLuceneIndex(oldDoce);
			}
		} else {
			this.farmFileIndexManagerImpl.handleFileLucenneIndexBymoveType(oldDoce, newDoce);
			this.farmDocIndexManagerImpl.reLuceneIndex(oldDoce, newDoce);
		}

		return newDoce;
	}

	private void editDocAuditHandle(FarmDoctype newType, FarmDoctype oldType, DocEntire newDoce, DocEntire paradoce,
			LoginUser user, String editNote) {
		FarmDoctext newText = null;
		if (newType.getId().equals(oldType.getId())) {
			newText = this.insertDocTextVersionForTemp(newDoce, paradoce, user, editNote);
		} else {
			newText = this.insertDocTextVersionForTemp(newDoce, paradoce, user, editNote);
			newDoce.getDoc().setState("2");
			this.farmDocDao.editEntity(newDoce.getDoc());
			this.farmDocIndexManagerImpl.delLuceneIndex(paradoce);
		}

		DocAudit audit = this.farmTypePopServerImpl.auditHandleForDocEdit(paradoce.getDoc().getId(), newText.getId(),
				user);
		newDoce.setAudit(audit);
		newDoce.setAuditTemp(newText);
		this.usermessageServiceImpl.sendMessage(this.farmDocTypeManagerImpl.getPopTypeAudtUserIds(newType.getId()),
				this.farmTypePopServerImpl.getMessageForNeedAudit(newDoce.getDoc().getTitle()),
				this.farmTypePopServerImpl.getMessageTitleForNeedAudit(newDoce.getDoc().getTitle()));
	}

	private FarmDoctext insertDocTextVersionForCurrent(DocEntire dbText, DocEntire newText, LoginUser user,
			String message) {
		FarmDoctext text = this.farmDoctextDao.getEntity(dbText.getDoc().getTextid());

		try {
			FarmDoctext histext = (FarmDoctext) BeanUtils.cloneBean(text);
			histext.setId((String) null);
			histext.setPstate("2");
			histext.setDocid(dbText.getDoc().getId());
			this.farmDoctextDao.insertEntity(histext);
		} catch (Exception var7) {
			throw new RuntimeException(var7);
		}

		text.setText1(newText.getTexts().getText1());
		text.setCtime(TimeTool.getTimeDate14());
		text.setEtime(TimeTool.getTimeDate14());
		text.setCuser(user.getId());
		text.setPcontent(message);
		text.setCusername(user.getName());
		text.setEuser(user.getId());
		text.setEusername(user.getName());
		this.farmDoctextDao.editEntity(text);
		return newText.getTexts();
	}

	private FarmDoctext insertDocTextVersionForTemp(DocEntire oldText, DocEntire newText, LoginUser user,
			String message) {
		FarmDoctext text = this.farmDoctextDao.getEntity(oldText.getDoc().getTextid());

		try {
			if (text == null) {
				text = new FarmDoctext();
			}

			FarmDoctext histext = (FarmDoctext) BeanUtils.cloneBean(text);
			histext.setId((String) null);
			histext.setPstate("3");
			histext.setDocid(oldText.getDoc().getId());
			histext.setText1(newText.getTexts().getText1());
			histext.setCtime(TimeTool.getTimeDate14());
			histext.setEtime(TimeTool.getTimeDate14());
			histext.setCuser(user.getId());
			histext.setPcontent(message);
			histext.setCusername(user.getName());
			histext.setEuser(user.getId());
			histext.setEusername(user.getName());
			FarmDoctext newtext = this.farmDoctextDao.insertEntity(histext);
			return newtext;
		} catch (Exception var8) {
			throw new RuntimeException(var8);
		}
	}

	@Transactional
	public DocEntire deleteDoc(String docid, LoginUser user) throws CanNoDeleteException {
		Doc doc = this.farmDocDao.getEntity(docid);
		if (!this.farmDocOperate.isDel(user, doc)) {
			throw new CanNoDeleteException("当前用户无此权限！");
		} else {
			return this.deleteDocNoPop(docid, user);
		}
	}

	public DataQuery createSimpleDocQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery.init(query,
				"FARM_DOC  A LEFT JOIN FARM_RF_DOCTYPE B ON B.DOCID =A.ID LEFT JOIN FARM_DOCTYPE C ON C.ID=B.TYPEID LEFT JOIN farm_docgroup d ON d.ID=a.DOCGROUPID LEFT JOIN FARM_DOCRUNINFO e on e.id=a.RUNINFOID",
				"A.ID AS ID,A.DOCDESCRIBE as DOCDESCRIBE,A.WRITEPOP as WRITEPOP,A.READPOP as READPOP,A.TITLE AS TITLE,A.AUTHOR AS AUTHOR,A.PUBTIME AS PUBTIME,A.DOMTYPE AS DOMTYPE,A.SHORTTITLE AS SHORTTITLE,A.TAGKEY AS TAGKEY,A.STATE AS STATE,D.GROUPNAME AS GROUPNAME,C.NAME as TYPENAME,E.VISITNUM AS VISITNUM, E.HOTNUM AS HOTNUM, E.LASTVTIME AS LASTVTIME, E.PRAISEYES AS PRAISEYES, E.PRAISENO AS PRAISENO, E.EVALUATE AS EVALUATE, E.ANSWERINGNUM AS ANSWERINGNUM ");
		return dbQuery;
	}

	@Transactional
	public DocEntire getDoc(String docid) {
		if (docid != null && !docid.isEmpty()) {
			DocEntire doce = new DocEntire(this.farmDocDao.getEntity(docid));
			doce.setTexts(this.farmDoctextDao.getEntity(doce.getDoc().getTextid()));
			List<FarmDoctype> types = this.farmRfDoctypeDao.getDocTypes(docid);
			if (types.size() > 0) {
				doce.setType((FarmDoctype) types.get(0));
				doce.setCurrenttypes(this.farmDocTypeManagerImpl.getTypeAllParent(doce.getType().getId()));
			}

			doce.setRuninfo(this.farmDocruninfoDao.getEntity(doce.getDoc().getRuninfoid()));
			List pops;
			if (doce.getDoc().getTagkey() != null) {
				String tags = doce.getDoc().getTagkey();
				pops = FarmFormatUnits.parseTags(tags.trim());
				doce.setTags(pops);
			}

			if (doce.getDoc().getDocgroupid() != null) {
				doce.setGroup(this.farmDocgroupManagerImpl.getFarmDocgroup(doce.getDoc().getDocgroupid()));
				doce.getGroup().setImgurl(this.farmFileServer.getFileURL(doce.getGroup().getGroupimg()));
			}

			List<FarmDocfile> files = this.farmDocfileDao.getEntityByDocId(docid);
			Iterator var6 = files.iterator();

			while (var6.hasNext()) {
				FarmDocfile file = (FarmDocfile) var6.next();
				file.setUrl(this.farmFileServer.getFileURL(file.getId()));
			}

			doce.setFiles(files);
			if (doce.getDoc().getImgid() != null && doce.getDoc().getImgid().trim().length() > 0) {
				String url = DocumentConfig.getString("config.doc.download.url") + doce.getDoc().getImgid();
				doce.setImgUrl(url);
			}

			User user = new User();
			user.setId(doce.getDoc().getCuser());
			user.setName(doce.getDoc().getCusername());
			doce.setUser(user);
			if (doce.getType() != null) {
				pops = this.farmDocTypeManagerImpl.getTypePops(doce.getType().getId());
				List<Doctypepop> readpops = new ArrayList();
				List<Doctypepop> writepops = new ArrayList();
				List<Doctypepop> auditpops = new ArrayList();
				Iterator var10 = pops.iterator();

				while (var10.hasNext()) {
					Doctypepop node = (Doctypepop) var10.next();
					if (node.getFuntype().equals("1")) {
						readpops.add(node);
					}

					if (node.getFuntype().equals("2")) {
						writepops.add(node);
					}

					if (node.getFuntype().equals("3")) {
						auditpops.add(node);
					}
				}

				doce.setTypeReadPops(readpops);
				doce.setTypeWritePops(writepops);
				doce.setTypeAuditPops(auditpops);
			}

			return doce;
		} else {
			return null;
		}
	}

	@Transactional
	public DocEntire getDoc(String docid, LoginUser user) throws CanNoReadException, DocNoExistException {
		Doc docbean = this.farmDocDao.getEntity(docid);
		if (docbean == null) {
			this.farmDocIndexManagerImpl.delLuceneIndex(docid);
			throw new DocNoExistException();
		} else if (!this.farmDocOperate.isRead(user, docbean)) {
			throw new CanNoReadException();
		} else {
			DocEntire doc = this.getDoc(docid);
			return doc;
		}
	}

	@Transactional
	public Doc getDocOnlyBean(String id) {
		return this.farmDocDao.getEntity(id);
	}

	public DataQuery createSimpleTypeQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery.init(query, "farm_doctype a LEFT JOIN farm_doctype b ON a.PARENTID=b.id",
				"a.ID AS id,a.TAGSNUM as TAGSNUM,a.READPOP as READPOP,a.WRITEPOP as WRITEPOP,a.AUDITPOP as AUDITPOP,a.NAME AS NAME,a.TYPEMOD AS TYPEMOD,a.CONTENTMOD AS CONTENTMOD,a.SORT AS SORT,a.TYPE AS TYPE, a.METATITLE AS METATITLE,a.METAKEY AS METAKEY,a.METACONTENT AS METACONTENT,a.LINKURL AS LINKURL,a.PCONTENT AS PCONTENT,a.PSTATE AS PSTATE,A.DOCNUM AS DOCNUM,A.QUESTIONNUM AS QUESTIONNUM");
		return dbQuery;
	}

	@Transactional
	public void updateDocTypeOnlyOne(String docid, String typeId) {
		List<DBRule> list = new ArrayList();
		list.add(new DBRule("DOCID", docid, "="));
		this.farmRfDoctypeDao.deleteEntitys(list);
		this.farmRfDoctypeDao.insertEntity(new FarmRfDoctype(typeId, docid));
	}

	@Transactional
	public List<FarmDoctext> getDocVersions(String docId) {
		FarmDoctext docText = this.farmDoctextDao.getEntity(this.getDocOnlyBean(docId).getTextid());
		DataQuery dbQuery = DataQuery.getInstance("1", "ID,ETIME,CUSERNAME,DOCID,PCONTENT,PSTATE", "farm_doctext");
		dbQuery.addRule(new DBRule("PSTATE", "1", "!="));
		dbQuery.addRule(new DBRule("DOCID", docId, "="));
		dbQuery.addSqlRule("and PSTATE !='3'");
		dbQuery.setPagesize(100);
		dbQuery.addSort(new DBSort("CTIME", "DESC"));
		dbQuery.setNoCount();
		List list = new ArrayList();
		list.add(docText);

		try {
			list.addAll(dbQuery.search().getObjectList(FarmDoctext.class));
			return list;
		} catch (SQLException var6) {
			throw new RuntimeException();
		}
	}

	@Transactional
	public DocEntire getDocVersion(String textId, LoginUser currentUser) {
		if (textId == null) {
			return null;
		} else {
			FarmDoctext text = this.farmDoctextDao.getEntity(textId);
			DocEntire doc = new DocEntire(this.farmDocDao.getEntity(text.getDocid()));
			if (doc.getDoc().getReadpop().equals("0") && !doc.getDoc().getCuser().equals(currentUser.getId())) {
				throw new RuntimeException("没有阅读权限");
			} else {
				doc.setTexts(text);
				List<FarmDoctype> doctypes = this.farmRfDoctypeDao.getDocTypes(doc.getDoc().getId());
				if (doctypes.size() > 0) {
					doc.setType((FarmDoctype) doctypes.get(0));
					doc.setCurrenttypes(
							this.farmDocTypeManagerImpl.getTypeAllParent(((FarmDoctype) doctypes.get(0)).getId()));
				}

				if (doc.getDoc().getTagkey() != null) {
					String tags = doc.getDoc().getTagkey();
					List<String> tags1 = FarmFormatUnits.parseTags(tags.trim());
					doc.setTags(tags1);
				}

				return doc;
			}
		}
	}

	@Transactional
	public void delImg(String imgid) {
		List<Doc> docList = this.farmDocDao.selectEntitys((new DBRule("IMGID", imgid, "=")).getDBRules());
		Iterator var4 = docList.iterator();

		while (var4.hasNext()) {
			Doc doc = (Doc) var4.next();
			doc.setImgid((String) null);
		}

		this.farmRfDoctextfileDao.deleteEntitys((new DBRule("FILEID", imgid, "=")).getDBRules());
		this.farmFileServer.delFile(imgid, (LoginUser) null);
	}

	@Transactional
	public DocEntire deleteDocNoPop(String docid, LoginUser user) throws CanNoDeleteException {
		List<DBRule> delRelationlist1 = new ArrayList();
		delRelationlist1.add(new DBRule("DOCID1", docid, "="));
		this.farmDocRelationDao.deleteEntitys(delRelationlist1);
		List<DBRule> delRelationlist2 = new ArrayList();
		delRelationlist2.add(new DBRule("DOCID2", docid, "="));
		this.farmDocRelationDao.deleteEntitys(delRelationlist2);
		DocEntire doc = this.getDoc(docid);
		FarmDoctext text = this.farmDoctextDao.getEntity(doc.getDoc().getTextid());
		this.farmTypePopServerImpl.delAuditInfo(doc.getDoc().getId());
		this.farmtopDaoImpl.deleteEntitys((new DBRule("docid", docid, "=")).getDBRules());
		List<DBRule> joylist = new ArrayList();
		joylist.add(new DBRule("DOCID", doc.getDoc().getId(), "="));
		this.farmDocenjoyDao.deleteEntitys(joylist);
		List<DBRule> rulesDelType = new ArrayList();
		rulesDelType.add(new DBRule("DOCID", doc.getDoc().getId(), "="));
		this.farmRfDoctypeDao.deleteEntitys(rulesDelType);
		this.farmDocDao.deleteEntity(doc.getDoc());
		List<DBRule> rulesDelFile = new ArrayList();
		rulesDelFile.add(new DBRule("DOCID", doc.getDoc().getId(), "="));
		List<FarmRfDoctextfile> files = this.farmRfDoctextfileDao.selectEntitys(rulesDelFile);
		Iterator var12 = files.iterator();

		while (var12.hasNext()) {
			FarmRfDoctextfile node = (FarmRfDoctextfile) var12.next();
			this.farmFileServer.delFile(node.getFileid(), user);
		}

		List<DBRule> rulesDelText = new ArrayList();
		rulesDelText.add(new DBRule("DOCID", doc.getDoc().getId(), "="));
		this.farmRfDoctextfileDao.deleteEntitys(rulesDelText);
		if (doc.getDoc().getImgid() != null && doc.getDoc().getImgid().trim().length() > 0) {
			this.farmDocfileDao.deleteEntity(this.farmDocfileDao.getEntity(doc.getDoc().getImgid()));
		}

		List<DBRule> rulesDelruninfoDetail = new ArrayList();
		rulesDelruninfoDetail.add(new DBRule("RUNINFOID", doc.getDoc().getRuninfoid(), "="));
		this.farmDocruninfoDetailDao.deleteEntitys(rulesDelruninfoDetail);
		this.farmDocruninfoDao.deleteEntity(this.farmDocruninfoDao.getEntity(doc.getDoc().getRuninfoid()));
		if (text != null) {
			this.farmDoctextDao.deleteEntity(text);
		}

		if (doc.getType() != null) {
			this.farmDocTypeManagerImpl.refreshDocNum(doc.getType().getId());
		}

		this.farmDocIndexManagerImpl.delLuceneIndex(doc);
		return doc;
	}

	public DataQuery createAllPubDocQuery(DataQuery query) {
		DataQuery dbQuery = DataQuery.init(query,
				"FARM_DOC  A LEFT JOIN FARM_RF_DOCTYPE B ON B.DOCID =A.ID LEFT JOIN FARM_DOCTYPE C ON C.ID=B.TYPEID LEFT JOIN farm_docgroup d ON d.ID=a.DOCGROUPID",
				"A.ID AS ID,A.DOCDESCRIBE as DOCDESCRIBE,A.WRITEPOP as WRITEPOP,A.READPOP as READPOP,A.TITLE AS TITLE,A.AUTHOR AS AUTHOR,A.PUBTIME AS PUBTIME,A.DOMTYPE AS DOMTYPE,A.SHORTTITLE AS SHORTTITLE,A.TAGKEY AS TAGKEY,A.STATE AS STATE,D.GROUPNAME AS GROUPNAME ");
		dbQuery.addRule(new DBRule("c.READPOP", "0", "="));
		dbQuery.addRule(new DBRule("a.READPOP", "1", "="));
		return dbQuery;
	}

	@Transactional
	public void move2Type(String docIds, String typeId) {
		if (docIds != null && !docIds.isEmpty()) {
			if (typeId != null && !typeId.isEmpty()) {
				String[] docIdsArr = docIds.split(",");
				String[] var7 = docIdsArr;
				int var6 = docIdsArr.length;

				for (int var5 = 0; var5 < var6; ++var5) {
					String docId = var7[var5];
					List<FarmRfDoctype> list = this.farmRfDoctypeDao
							.selectEntitys((new DBRule("DOCID", docId, "=")).getDBRules());
					Iterator var10 = list.iterator();

					while (var10.hasNext()) {
						FarmRfDoctype entity = (FarmRfDoctype) var10.next();
						this.farmDocIndexManagerImpl.delLuceneIndex(this.getDoc(docId));
						this.farmRfDoctypeDao.deleteEntity(entity);
					}

					this.farmRfDoctypeDao.insertEntity(new FarmRfDoctype(typeId, docId));
					DocEntire doc = this.getDoc(docId);
					FarmDoctype type = this.farmDoctypeDao.getEntity(typeId);
					doc.setType(type);
					doc.setCurrenttypes(this.farmDocTypeManagerImpl.getTypeAllParent(typeId));
					this.farmDocIndexManagerImpl.addLuceneIndex(doc);
				}

			} else {
				throw new RuntimeException("无法获取参数：typeId");
			}
		} else {
			throw new RuntimeException("无法获取参数：docIds");
		}
	}
}
